package com.atguigu.flink.chapter09;

import com.atguigu.flink.bean.WaterSensor;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @author liuyun
 * @title: Flink03_CEP_Combine
 * @projectName flink0924
 * @description: TODO
 * @date 2022-3-9 19:10
 */
public class Flink03_CEP_Combine {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> stream = env.fromElements(
                        new WaterSensor("sensor_1", 1L, 10),
                        new WaterSensor("sensor_1", 2L, 20),
                        new WaterSensor("sensor_2", 3L, 30),
                        new WaterSensor("sensor_1", 4L, 40),
                        new WaterSensor("sensor_2", 5L, 50),
                        new WaterSensor("sensor_1", 6L, 60)
                )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((element, recordTimestamp) -> element.getTs())
                );
        Pattern<WaterSensor, WaterSensor> pattern = Pattern
                .<WaterSensor>begin("s1")
                .where(
                        new SimpleCondition<WaterSensor>() {
                            @Override
                            public boolean filter(WaterSensor value) throws Exception {
                                return "sensor_1".equals(value.getId());
                            }
                        }
                ).or(new SimpleCondition<WaterSensor>() {
                    @Override
                    public boolean filter(WaterSensor value) throws Exception {
                        return value.getVc() > 20;
                    }
                });

        PatternStream<WaterSensor> ps = CEP.pattern(stream, pattern);
        ps
                .select(new PatternSelectFunction<WaterSensor, String>() {

                    @Override
                    public String select(Map<String, List<WaterSensor>> map) throws Exception {
                        return map.toString();
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
